package com.yao.spider.shanghai;

import com.alibaba.fastjson.JSON;
import com.yao.spider.shanghai.bean.FastMail;
import com.yao.spider.shanghai.bean.PersonInfo;
import com.yao.spider.shanghai.bean.SubFastMail;
import com.yao.spider.shanghai.bean.SubPersonInfo;
import net.sf.json.util.JSONUtils;

public class JsonConvert {
    public static String person_string = "{\"_id\":\"AXtasW-oxRtHEfZe7Vr8\",\"_index\":\"person_address_label_info_master\",\"_score\":null,\"_source\":{\"AGE\":85,\"BIRTHDAY\":\"1936\",\"BPLACE\":\"江苏省苏州市沙洲县\",\"EDEGREE\":\"文盲或半文盲\",\"ESCU\":\"未服兵役\",\"HEIGHT\":\"154\",\"HHPLACE\":\"江苏省张家港市杨舍镇农联村乌沙六组25号\",\"IDNO\":\"320521193603134823\",\"IDTYPE\":\"01\",\"MARR\":\"已婚\",\"NPLACE\":\"江苏张家港市\",\"PHOTO\":\"{\"身份证\":[\"http://oss-cn-shanghai-shga-d01-a.ops.ga.sh/shga-ryzp/CSJ/JIANGSU_CZRK_ZP/iQv0KmQBKwsWAszfDf4o.jpg\"]}\",\"PROF\":\"棉农\",\"QUERY_STRING\":\" 江苏张家港市 江苏省苏州市沙洲县  江苏省张家港市杨舍镇农联村乌沙六组25号 85 36 1936 \",\"RNAME\":\"宋林珍\",\"SEX\":\"女\"},\"_type\":\"a\",\"sort\":[6756967]}";
    public static String person_string2 = "{\"_id\":\"AXtapGbijcEAuoI0lDsp\",\"_index\":\"person_address_label_info_master\",\"_score\":null,\"_source\":{\"AGE\":48,\"BIRTHDAY\":\"1973\",\"BPLACE\":\"吉林省吉林市磐石县\",\"IDNO\":\"22022319730217702x\",\"IDTYPE\":\"01\",\"QUERY_STRING\":\"  吉林省吉林市磐石县   48 73 1973 \",\"RNAME\":\"崔太花\",\"SEX\":\"女\"},\"_type\":\"a\",\"sort\":[6754190]}\n";
    public static String address = "{\"_source\":{\"APP_TYPE\":\"1240007\",\"ATTR_IDENT_LABEL\":\"07\",\"ATTR_IDENT_LABEL_DETL\":\"{\"LABEL_DETL\":[{\"07\":\"shga_wa.ods_nb_app_icpoof_delivery\"}]}\",\"BIG_SOURCE\":\"WA\",\"COUNT\":\"15\",\"DATA_SOURCE\":\"115\",\"DETAIL\":\"{\"nameinfo\":[{\"name\":\"樊**\"},{\"name\":\"樊*\"}]}\",\"FIRST_TIME\":\"1597727794\",\"IDENTITY_TYPE\":\"mobile\",\"IDENTITY_VALUE\":\"15500237753\",\"LAST_TIME\":\"1610872063\",\"MRG_ID\":\"1367c213df0218a7f9bff17be627ecb7\",\"SRC_ADDRESS\":\"吉林省白山市临江市吉林省白山市临江市大栗子快递超市\",\"SRC_ID\":\"1367c213df0218a7f9bff17be627ecb7\",\"TABLE_SOURCE\":\"shga_wa.ods_nb_app_icpoof_delivery\"},\"_type\":\"a\",\"sort\":[33152]}";


    public static PersonInfo person(String personString) {
        SubPersonInfo subPersonInfo = JSON.parseObject(fixJson(personString), SubPersonInfo.class);
        return subPersonInfo.get_source();
    }

    public static FastMail fastMail(String personString) {
        SubFastMail subPersonInfo = JSON.parseObject(fixFastMail(personString), SubFastMail.class);
        return subPersonInfo.get_source();
    }

    public static String fixJson(String info) {
        StringBuilder op = new StringBuilder(info);
        int photo1 = op.indexOf("PHOTO");
        if (photo1 < 0) {
            return info;
        }

        int i = op.indexOf("}", photo1);
        StringBuilder replace = op.replace(photo1 - 1, i + 3, "");
        System.out.println(replace.toString());
        return replace.toString();
    }
    public static String fixFastMail(String info) {
        StringBuilder op = new StringBuilder(info);
        int photo1 = op.indexOf("LABEL_DETL");
        if (photo1 > 0) {
            int i = op.indexOf("}", photo1);
            op.replace(photo1 - 1, i + 3, "");
        }
        int labelDetl = op.indexOf("ATTR_IDENT_LABEL");
        if (labelDetl > 0) {
//            int i = op.indexOf("}", labelDetl);
            int i = op.indexOf("BIG_SOURCE");
            op.replace(labelDetl - 1, i - 1, "");
        }
        int nameinfo = op.indexOf("DETAIL");
        if (nameinfo > 0) {
            int first_time = op.indexOf("FIRST_TIME");
            String substring = op.substring(nameinfo, first_time - 2);
            String replace = substring.replace("\"", "").replace(",", "").replace("\\{", "")
                    .replace("\\}", "")
                    .replace("\\[", "")
                    .replace("\\]", "")
                    .replace("DETAIL", "");
            String name = "\"name\":\"" + replace + "\",";
            op.replace(nameinfo - 1, first_time - 1, name);
        }
        System.out.println(op.toString());
        return op.toString();
    }

    public static void main(String[] args) {
        System.out.println(fastMail(address));
//        FastMail fastMail = fastMail(address);
        System.out.println();
    }
}
